const api = {

  /**
   * 获取当前日期
   */
  currentDate: () => {
    const date = new Date();
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    return `${year}-${month}-${day}`;
  },

  /**
   * 获取当前时间
   */
  currentTime: () => {
    const date = new Date();
    const hh = date.getHours();
    const mm = date.getMinutes();
    return `${hh}:${mm}`;
  },

  // 时间转换分秒
  extractionTime(timestamp) {
    let date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear();
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() + ' ';
    let h = date.getHours();
    let m = date.getMinutes();
    let s = date.getSeconds();
    h = (h > 9 ? h : "0" + h) + ":";
    m = (m > 9 ? m : "0" + m) + ":";
    s = s > 9 ? s : "0" + s;
    let str = [Y, M, D, h, m, s]
    return str;
  },
  // 时间转换分秒
  showConversTime(timestamp, type) {
    let date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '/';
    let Y1 = date.getFullYear() + '-';
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/';
    let M1 = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() + ' ';
    let h = date.getHours();
    let m = date.getMinutes();
    let s = date.getSeconds();
    h = (h > 9 ? h : "0" + h) + ":";
    m = (m > 9 ? m : "0" + m) + ":";
    s = s > 9 ? s : "0" + s;
    let str = m + s;
    if (type == 1) {
      str = `${M}${D} ${h}${m}${s}`
    }
    if (type == 2) {
      str = `${Y}${M}${D}`
    }
    if (type == 3) {
      str = `${Y1}${M1}${D} ${h}${m}${s}`
    }
    return str;
  },
  // 计时器 时间格式转换
  countdownTime(timestamp, type) {
    timestamp = Number(timestamp)
    let d, h, m, s;
    if (timestamp >= 0) {
      d = Math.floor(timestamp / 1000 / 60 / 60 / 24);
      h = Math.floor((timestamp / 1000 / 60 / 60) % 24);
      m = Math.floor((timestamp / 1000 / 60) % 60);
      s = Math.floor((timestamp / 1000) % 60);
      h = h > 9 ? h : '0' + h;
      m = m > 9 ? m : '0' + m;
      s = s > 9 ? s : '0' + s;
    }
    let str = `${d}天 ${h}:${m}:${s}`;
    if (type == 1 || type == 3) {
      h = Math.floor((timestamp / 1000 / 60 / 60)) > 9 ? Math.floor((timestamp / 1000 / 60 / 60)) : '0' + Math.floor((timestamp / 1000 / 60 / 60));
      if (type == 1) {
        str = `${h}:${m}:${s}`;
      }
      if (type == 3) {
        m = Math.floor(timestamp / 1000 / 60);
        str = '';
        if (Number(m) && Number(m) > 0) {
          str += `${m}分`
        }
        str += `${s}秒`
      }
    }
    if (type == 2) {
      if (d <= 0) {
        str = `${h}:${m}:${s}`
      }
      if (d >= 99) {
        str = `99天+`
      }
    }
    if (type == 4) {
      str = `${h}时${m}分${s}秒`;

      if (d > 0) {
        str = `${d}天 ` + str
      }
    }
    return str
  },
  /*
  对比当前时间
  * s2 ：判断时间
  * type ：1 时间戳
    */
  dateContrast(s2, type) {
    //获取当前时间
    var date = new Date();
    var now = date.getTime();
    //设置截止时间
    var endDate = new Date(s2.replace(/-/g, "/"));
    var end = endDate.getTime();
    if (type == 1) {
      end = s2
    }
    var leftTime = end - now;
    return leftTime;
  },
}

export default api;
